/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sistema.gestionuniversitaria.presentationLayer;

import com.sistema.gestionuniversitaria.BusinessLayer.Alumno;
import com.sistema.gestionuniversitaria.BusinessLayer.Carrera;
import com.sistema.gestionuniversitaria.BusinessLayer.Curso;
import com.sistema.gestionuniversitaria.dataLayer.AlumnoData;
import com.sistema.gestionuniversitaria.dataLayer.CarreraData;
import com.sistema.gestionuniversitaria.dataLayer.CursoData;
import com.sistema.gestionuniversitaria.dataLayer.ExcepcionGlobal;
import com.sistema.gestionuniversitaria.dataLayer.MatriculaData;
import com.sistema.gestionuniversitaria.dataLayer.NoDataException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Max
 */
public class IngresarCursoMatricula2 extends javax.swing.JInternalFrame {

    /**
     * Creates new form IngresarCursoMatricula2
     */
    CursoData curData = new CursoData();
    MatriculaData matData = new MatriculaData();
    CarreraData caData = new CarreraData();
    AlumnoData alData = new AlumnoData();
    String codCarrera;

    public IngresarCursoMatricula2() throws SQLException, ExcepcionGlobal, NoDataException {
        initComponents();
    }

    private void cargarListaCursos() throws SQLException, ExcepcionGlobal, NoDataException {
        codCarrera = getAlumno(jTbxCedula.getText().toUpperCase());
        ArrayList<Curso> cursos = curData.getCursosPorCarrera(codCarrera);
        for (Curso curso : cursos) {
            jComboCursos.addItem(curso);
        }
        jComboCursos.setEnabled(true);
    }

    private String getAlumno(String cedula) throws ExcepcionGlobal, NoDataException {
        Alumno al;
        String ced = null;
        al = alData.getAlumnoPOrCedula(cedula);
        ced = al.getCarrera().getCodigo();
        return ced;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTbxCedula = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jComboCursos = new javax.swing.JComboBox();
        jTbxSigla = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jTbxNombreCurso = new javax.swing.JTextField();
        jBtnAceptar = new javax.swing.JButton();
        jBtnCancelar = new javax.swing.JButton();
        jBtnBuscar = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setTitle("Ingresar Matricula");

        jLabel1.setText("Ingrese los datos correspondientes:");

        jLabel2.setText("Cedula Alumno:");

        jLabel5.setText("Eliga el nuevo curso a matricular:");

        jLabel6.setText("Curso:");

        jComboCursos.setEnabled(false);
        jComboCursos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboCursosActionPerformed(evt);
            }
        });

        jTbxSigla.setEnabled(false);

        jLabel7.setText("Sigla:");

        jLabel8.setText("Nombre:");

        jTbxNombreCurso.setEnabled(false);

        jBtnAceptar.setText("Aceptar");
        jBtnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnAceptarActionPerformed(evt);
            }
        });

        jBtnCancelar.setText("Cancelar");

        jBtnBuscar.setText("Comprobar");
        jBtnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnBuscarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBtnAceptar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jBtnCancelar))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jTbxCedula, javax.swing.GroupLayout.PREFERRED_SIZE, 235, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jLabel5)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel6)
                                .addComponent(jLabel7)
                                .addComponent(jLabel8))
                            .addGap(35, 35, 35)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jComboCursos, 0, 215, Short.MAX_VALUE)
                                .addComponent(jTbxSigla)
                                .addComponent(jTbxNombreCurso))))
                    .addComponent(jBtnBuscar))
                .addContainerGap(90, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTbxCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jBtnBuscar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                .addComponent(jLabel5)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jComboCursos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(jTbxSigla, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jTbxNombreCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBtnAceptar)
                    .addComponent(jBtnCancelar))
                .addGap(35, 35, 35))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboCursosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboCursosActionPerformed
        Curso cursoSeleccionado = (Curso) jComboCursos.getSelectedItem();
        Curso cursoRetornado = new Curso();
        String sigla = cursoSeleccionado.getSigla();
        try {
            cursoRetornado = curData.getCursoPorSigla(sigla);
        } catch (ExcepcionGlobal ex) {
            Logger.getLogger(IngresarCursoMatricula2.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoDataException ex) {
            Logger.getLogger(IngresarCursoMatricula2.class.getName()).log(Level.SEVERE, null, ex);
        }
        jTbxSigla.setText(cursoRetornado.getSigla());
        jTbxNombreCurso.setText(cursoRetornado.getNombre());
    }//GEN-LAST:event_jComboCursosActionPerformed

    private void jBtnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnAceptarActionPerformed
        try {
            if (!matData.verificaCursoMatricula(jTbxCedula.getText().toUpperCase(), jTbxSigla.getText())) {
                curData.insertarCursoMatricula(jTbxCedula.getText(), jTbxSigla.getText());
                JOptionPane.showMessageDialog(null, "Éxito en la ejecución");
            } else {
                JOptionPane.showMessageDialog(null, "El curso ya está matriculado");
            }
        } catch (ExcepcionGlobal ex) {
            Logger.getLogger(IngresarCursoMatricula.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoDataException ex) {
            Logger.getLogger(IngresarCursoMatricula.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(IngresarCursoMatricula.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jBtnAceptarActionPerformed

    private void jBtnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnBuscarActionPerformed
        try {
            cargarListaCursos();
        } catch (SQLException ex) {
            Logger.getLogger(IngresarCursoMatricula2.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ExcepcionGlobal ex) {
            Logger.getLogger(IngresarCursoMatricula2.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoDataException ex) {
            Logger.getLogger(IngresarCursoMatricula2.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jBtnBuscarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBtnAceptar;
    private javax.swing.JButton jBtnBuscar;
    private javax.swing.JButton jBtnCancelar;
    private javax.swing.JComboBox jComboCursos;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JTextField jTbxCedula;
    private javax.swing.JTextField jTbxNombreCurso;
    private javax.swing.JTextField jTbxSigla;
    // End of variables declaration//GEN-END:variables
}
